<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('关联功能配置列表')" />
</head>
<body>
<form>
	<div class="layuiadmin-header-box">
		<th:block th:include="listinclude :: searchTop" />
	</div>
</form>
<div class="mid-content">
    <table class="layui-hide" id="dhtable" lay-filter="ftable"></table>
</div>
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
        <div class="layui-btn layui-btn-normal layui-table-tools" onclick="$.operate.add('{{rModuleTable}}')" shiro:hasPermission="system:dictionaryformodule:add">
            <p class="dhicon dh-plus" style="color:white;font-size:14px"><span class="fa_span">新增</span></p>
        </div>
    </div>
</script>
<script type="text/html" id="barDh">
	<a class="layui-btn layui-btn-xs dhicon dh-edit" onclick="$.operate.edit('{{d.guid}}')"  shiro:hasPermission="system:dictionaryformodule:delete">&nbsp;编辑</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs dhicon dh-remove" onclick="remove('{{d.guid}}')" shiro:hasPermission="system:dictionaryformodule:delete">&nbsp;删除</a>
	<a class="layui-btn layui-btn-warm layui-btn-xs dhicon dh-search" onclick="relation('{{d.guid}}','{{d.sModuleTable}}','{{d.sModuleTableField}}','{{d.rModuleTable}}')" shiro:hasPermission="system:dictionaryrelationforfield:view">&nbsp;详细</a>
</script>
<script type="text/html" id="templet_bdeleted">
    <input type="checkbox" value="{{d.guid}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="bdeleted" {{ d.bdeleted? '' : 'checked' }}>
</script>
<script type="text/html" id="dropdown_menuGuid">
	<select name="menuGuid" id="menuGuid" th:with="type=${@baseinfosimple.getMenuFieldFrontConditions('notSet')}">
		<option value="">请选择</option>
		<option th:each="dict : ${type}" th:text="${dict.menuCaption}" th:value="${dict.guid}"></option>
	</select>	
</script>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
	/*<![CDATA[*/
	var prefix = ctx + "system/dictionaryrelationformodule";
	var prefix2 = ctx + "system/dictionaryrelationforfield";
	var gridColumns=[[${gridColumns}]];
	var rModuleTable=window.location.href.split("=")[1];
	var moduleTable=[[${moduleTable}]];
	/*]]>*/
</script>
<script type="text/javascript">
var $,layer,form,table,wherestr={};
layui.use(['table','form','layer','jquery'], function () {
     $ = layui.$,layer = layui.layer,form = layui.form,table = layui.table;
     //表单初始化
     $.tablelist.init({
         elem: '#dhtable'
       	 ,modalName: "关联功能"
         ,url: prefix + "/list?RModuleTable="+rModuleTable
         ,createUrl: prefix + "/add?rModuleTable={id}"
         ,updateUrl: prefix + "/edit?guid={id}"
         ,initSort: {field: 'relationOrder',type: 'asc'}
		,where: {
	    	field: 'relationOrder' //排序字段
	    	,order: 'asc' //排序方式
	    }
		 ,totalRow: gridColumns.totalRow
		 ,cols: [gridColumns.gridBody
		 ]
        ,toolbar: '#toolbar'
    	,defaultToolbar: [{title:'列设置',layEvent: 'fieldSet',icon: 'layui-icon-set'}] //这里在右边显示
        ,height:'full-90'
     });
	//监听排序事件 
     table.on('sort(ftable)', function(obj){
     	$.tablelist.sort('dhtable',obj);
     });
   	//头工具栏事件
  	table.on('toolbar(ftable)', function(obj){
  	    switch(obj.event){
  	    	case 'fieldSet':
  	    		filedSet(moduleTable);
           	break;
  	    };
  	});
  	//搜索按钮
 	$('#btn-search').on('click', function () {
 		$.each(wherestr,function(i,v){$("#"+i).val()?wherestr[i]=$("#"+i).val():wherestr[i]='';});//动态获取条件的值
        table.reload('dhtable', {page: {curr: 1},where:wherestr});
     });
 	//监听下拉框——添加条件
 	form.on('select(fft)', function(obj){
 		addMore(this);
 	});
 	//监听开关动作
	form.on('switch(bdeleted)', function(obj){
		if(obj.elem.checked==true){
  	    	$.operate.post(prefix + "/editpost", {'dictionaryRelationForModuleItems':JSON.stringify([{"guid":obj.value,"bdeleted":"0"}])}); 
    	}else{
	    	$.operate.post(prefix + "/editpost", {'dictionaryRelationForModuleItems':JSON.stringify([{"guid":obj.value,"bdeleted":"1"}])});
	    }
    });
});
//删除
function remove(guid){
	$.operate.remove("确定要删除么？",prefix + "/delete",{'dictionaryRelationForModuleItems':JSON.stringify([{'guid':guid}])},function(result){
		if(result.code==0){
	        setTimeout(function(){
	            layer.closeAll();
	            table.reload('dhtable');
	        }, 1000);
	    }
	});
}
function relation(guid,sModuleTable,sModuleTableField,rModuleTable){
	if (sModuleTableField == "null" || sModuleTableField == "" || sModuleTableField == null){
		sModuleTable = sModuleTable;
	}else{
		sModuleTable = sModuleTableField;
	}
	$.modal.open('调入关系',prefix2 + "/field?guid="+guid+'='+sModuleTable+'='+rModuleTable,'1200','700',function(index){
        var res = window["layui-layer-iframe" + index].callbackdata();
        if(res=="重复"){
        	layer.msg("出现重复记录！");
        }else if(res.length > 0){
        	var newDatas=new Array(),oldDatas=new Array(),delDatas=new Array();
        	$.each(res,function(i,v){
        		if(v.newData){
        			newDatas.push(v);
        		}else if(v.del){
        			delDatas.push(v);
        		}else{
        			oldDatas.push(v);
        		}
        	});
        	layer.closeAll();
        	if(delDatas.length!=0)$.operate.post(prefix2 + "/delete", {'dictionaryRelationForFieldItems':JSON.stringify(delDatas, null, 2)});
        	if(oldDatas.length!=0)$.operate.post(prefix2 + "/editpost", {'dictionaryRelationForFieldItems':JSON.stringify(oldDatas, null, 2)});
        	if(newDatas.length!=0)$.operate.post(prefix2 + "/addpost", {'dictionaryRelationForFieldItems':JSON.stringify(newDatas, null, 2)});
        }else{
            layer.msg("无数据操作！");
        }
    });
}

</script>
</body>
</html>